package com.ruoyi.order.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.order.es.EsProd;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.TbProdMapper;
import com.ruoyi.order.domain.TbProd;
import com.ruoyi.order.service.ITbProdService;

/**
 * 商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-02
 */
@Service
public class TbProdServiceImpl implements ITbProdService 
{
    @Autowired
    private TbProdMapper tbProdMapper;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     * 查询商品
     * 
     * @param prodId 商品主键
     * @return 商品
     */
    @Override
    public TbProd selectTbProdByProdId(Long prodId)
    {
        return tbProdMapper.selectTbProdByProdId(prodId);
    }

    /**
     * 查询商品列表
     * 
     * @param tbProd 商品
     * @return 商品
     */
    @Override
    public List<TbProd> selectTbProdList(TbProd tbProd)
    {
        return tbProdMapper.selectTbProdList(tbProd);
    }

    /**
     * 新增商品
     * 
     * @param tbProd 商品
     * @return 结果
     */
    @Override
    public int insertTbProd(TbProd tbProd)
    {
        tbProd.setCreateTime(DateUtils.getNowDate());
        return tbProdMapper.insertTbProd(tbProd);
    }

    /**
     * 修改商品
     * 
     * @param tbProd 商品
     * @return 结果
     */
    @Override
    public int updateTbProd(TbProd tbProd)
    {
        tbProd.setUpdateTime(DateUtils.getNowDate());
        return tbProdMapper.updateTbProd(tbProd);
    }

    /**
     * 批量删除商品
     * 
     * @param prodIds 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteTbProdByProdIds(Long[] prodIds)
    {
        return tbProdMapper.deleteTbProdByProdIds(prodIds);
    }

    /**
     * 删除商品信息
     * 
     * @param prodId 商品主键
     * @return 结果
     */
    @Override
    public int deleteTbProdByProdId(Long prodId)
    {
        return tbProdMapper.deleteTbProdByProdId(prodId);
    }

    @Override
    public AjaxResult createProdIndex() {
        IndexOperations indexOps = elasticsearchRestTemplate.indexOps(EsProd.class);
        boolean exists = indexOps.exists();
        if (exists){
            indexOps.delete();
        }
        indexOps.create();
        Document mapping = indexOps.createMapping();
        indexOps.putMapping(mapping);
        return null;
    }

    @Override
    public AjaxResult syncProdIndex() {
        List<TbProd> tbProds = tbProdMapper.selectTbProdList(new TbProd());
        for (TbProd tbProd : tbProds) {
            EsProd esProd = BeanUtil.toBean(tbProd, EsProd.class);
            elasticsearchRestTemplate.save(esProd);
        }
        return AjaxResult.success("成功");
    }

    @Override
    public TableDataInfo getListFromEs(TbProd tbProd, Integer pageNum, Integer pageSize) {
        Integer from = 0;
        if (pageNum>0){
            from=(pageNum-1);
        }
        PageRequest pageRequest = PageRequest.of(from, pageSize);
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(QueryBuilders.matchAllQuery());
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        nativeSearchQueryBuilder.withPageable(pageRequest).withQuery(boolQueryBuilder);
        SearchHits<EsProd> searchHits = elasticsearchRestTemplate.search(nativeSearchQueryBuilder.build(), EsProd.class);
        ArrayList<EsProd> list = new ArrayList<>();
        for (SearchHit<EsProd> searchHit : searchHits.getSearchHits()) {
            list.add(searchHit.getContent());
        }
        TableDataInfo repData = new TableDataInfo();
        repData.setCode(HttpStatus.SUCCESS);
        repData.setRows(list);
        repData.setMsg("查询成功");
        repData.setTotal(searchHits.getTotalHits());
        return repData;
    }
}
